<template>
  <div
    ref="swiper"
    class="swiper-container"
    @mouseenter="swiper.autoplay.stop()"
    @mouseout="swiper.autoplay.start()"
  >
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in imagelist" :key="item.id">
        <img :src="item.imgUrl" :alt="item.id" />
      </div>
    </div>
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";

import "../../../node_modules/swiper/css/swiper.css";
export default {
  props: {
    imagelist: {
      type: Array,
      required: true,
    },
  },
  watch: {
    //触发说明数据更新了 传进来了
    imagelist: {
      handler() {
        if (!this.imagelist.length) return;

        // 这里说明页面好了
        this.$nextTick(() => {
          this.swiper = new Swiper(this.$refs.swiper, {
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
            pagination: {
              el: ".swiper-pagination",
            },
            loop: true, // 无缝轮播
            autoplay: true, // 自动轮播
          });
        });
      },
      // 因为在floor组件中使用 一开始就有值 所以不会改变
      // 需要添加immediate为真 让它上来就触发一次
      immediate: true, // 让watch一上来触发
    },
  },
};
</script>

<style></style>
